﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Security;

namespace TVXD23.Web.Models.Helpers
{
    public static class TagExpandHelper
    {
        public static string LinkPagingForAnphabetMember(this HtmlHelper helper, int pageSize, char kt, int page, int count)
        {
            int n = count / 5 + 1;
            string link = "<a href=\"?currentPage=1&kt=A\"/>First</a>&nbsp;&nbsp;";
            for (int i = 1; i <= n; i++)
            {
                string s = string.Format("<a href=\"?currentPage={0}&kt={1}\">[{2}]</a>", i, kt, i);
                link += s + "&nbsp;&nbsp;";
            }
            string curr = string.Format("[{0}]", page);
            string currReplace = string.Format("<b>[{0}]</b>", page);
            return link.Replace(curr, currReplace);
        }
        public static string RenderImageName(this HtmlHelper helper, string name)
        {
            if (name == null) return "";
            if (name.Length < 16)
                return name;
            string s = name.Substring(0, 13) + "...";
            return s;
        }
        public static string RenderDescription(this HtmlHelper helper, string description)
        {
            if (description != null)
                if (description.Length < 250)
                    return description;
                else
                {
                    return description.Substring(0, 250);

                }
            return "";



        }
        public static string ImageLink(this AjaxHelper helper, string imgUrl, string altText, string actionName, string controllerName, object routes, AjaxOptions ajaxOptions)
        {
            TagBuilder imgLink = new TagBuilder("a");
            var s = routes.ToString().Replace("{", "?").Replace("}", "");
            imgLink.MergeAttribute("href", "/" + controllerName + "/" + actionName + s);
            imgLink.MergeAttribute("onclick", "Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: 'MemberDetails' });");
            imgLink.InnerHtml = "Chi";
            return imgLink.ToString();

            /*
              <a href="/Admin/MemberDetails?userName=My%20MVC%20%20%20%20%20%20%20%20%20%
             * 20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2
             * 0%20%20%20%20%20%20%20" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEve
             * nt(event), { insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: 'MemberDetails' });">D</a>
              */
        }
        public static string ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes)
        {
            UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
            TagBuilder imgTag = new TagBuilder("img");
            imgTag.MergeAttribute("src", imgSrc);
            imgTag.MergeAttributes((IDictionary<string, string>)imgHtmlAttributes, true);
            string url = urlHelper.Action(actionName, controllerName, routeValues);



            TagBuilder imglink = new TagBuilder("a");
            imglink.MergeAttribute("href", url);
            imglink.InnerHtml = imgTag.ToString();
            imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true);

            return imglink.ToString();

        }
        //public static string RoleNameOfMember(this HtmlHelper helper, string userName)
        //{
        //    UEFRepository repository = new UEFRepository();
        //    return repository.RoleNameOfMember(userName);
        //}
        //public static string ProfilePageLink(this HtmlHelper helper, string userName)
        //{
        //    UEFRepository re = new UEFRepository();
        //    var a = re.FindMemberById(userName);
        //    if (a.DisplayName != "")
        //    {
        //        return string.Format("<a href=\"/Member.aspx/MemberProfile?userID={0}\">{1}</a>", userName, a.DisplayName);

        //    }
        //    return string.Format("<a href=\"/Member.aspx/MemberProfile?userID={0}\">{1}</a>", userName, a.FullName);

        //}
        //public static string TotalPostByUser(this HtmlHelper helper, string userName)
        //{
        //    UEFRepository re = new UEFRepository();
        //    return re.NumberOfPressPostByMember(userName).ToString();
        //}
        public static string PostOnDay(this HtmlHelper helper, string userName)
        {
            return "3/5";
        }
        public static string LastLogon(this HtmlHelper helper, string userName)
        {
            AccountMembershipService m = new AccountMembershipService();
            return Membership.GetUser(userName).LastLoginDate.ToShortDateString();

        }

        public static string YearOption(this HtmlHelper helper, int Year)
        {
            TagBuilder tag = new TagBuilder("option");

            tag.MergeAttribute("value", Year.ToString());
            tag.SetInnerText(Year.ToString());

            return tag.ToString(TagRenderMode.Normal);
        }
        public static string PressTitleLinkHtml(this HtmlHelper helper, string pressTitle, int id, string lang)
        {
            pressTitle = pressTitle.Replace("?", "");
            pressTitle = pressTitle.Replace("&", "");
            string tt = Convert_Chuoi_Khong_Dau(pressTitle);
            string s = string.Format("<a href=\"/Home/ViewPressDetails/{0}/{1}/{2}\">{3}</a>", id, lang, tt, pressTitle);
            return s;
        }
        public static string PressTitleLinkInnerHtml(this HtmlHelper helper, string pressTitle, int id)
        {
            pressTitle = pressTitle.Replace("?", "");
            string tt = Convert_Chuoi_Khong_Dau(pressTitle).Replace(" ", "-");
            string s = string.Format("/Press/PressDetails/{0}/{1}", id, tt);
            return s;
        }
        public static string Convert_Chuoi_Khong_Dau(string s)
        {
            s = s.Trim().Replace("  ", " ").Replace(",", "");
            s = s.Replace(" ", "-");
            s = s.Replace(":", "-");
            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = s.Normalize(NormalizationForm.FormD);
            string result = regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
            result = result.Replace(":", "");
            return result;
        }
        public static string ReadMorePressLinkHtml(this HtmlHelper helper, string pressTitle, int id, string lang)
        {
            pressTitle = pressTitle.Replace("?", "");
            string tt = Convert_Chuoi_Khong_Dau(pressTitle);
            string s = string.Format("<a href=\"/Home/ViewPressDetails/{0}/{1}/{2}\">xem tiếp</a>", id, lang, tt);
            return s;
        }
        public static string ReadMorePressLinkHtmlen(this HtmlHelper helper, string pressTitle, int id, string lang)
        {
            pressTitle = pressTitle.Replace("?", "");
            string tt = Convert_Chuoi_Khong_Dau(pressTitle);
            string s = string.Format("<a href=\"/Home/ViewPressDetails/{0}/{1}/{2}\">read more</a>", id, lang, tt);
            return s;
        }
        public static string FilterImageLink(this HtmlHelper helper, string s)
        {
            int n = s.IndexOf("<img");
            string a = "";
            if (n == -1)
                return "<img src=\"http://www.google.com.vn/logos/2011/houdini11-sr.jpg\"/>";
            for (int i = n; i < s.Length; i++)
            {
                if (s[i + 1].ToString() == ">")
                    break;
                a += s[i].ToString();
            }

            return a += "/>";

        }
        //public static string AjaxPagingLinkFollow(this AjaxHelper helper,int currentPressPage,int totalItems,string userID,int fatherPage,string action,string controller,string IdToUpdate)
        //{
        //    string chuoi = "";
        //    for(int i=1;i<totalItems/5+1;i++)
        //    {
        //        string s = helper.ActionLink(i.ToString(), action, controller,
        //                                     new { currentPage = fatherPage, currentPageOfPress = i, userID },
        //                                     new AjaxOptions
        //                                         {UpdateTargetId = IdToUpdate, InsertionMode = InsertionMode.Replace}).ToString();
        //        chuoi +=" "+ s;
        //    }
        //    chuoi = chuoi.Replace("href=\""+currentPressPage.ToString(), "href=\"[" + currentPressPage.ToString() + "]");
        //    return chuoi;
        //}
        //public static string FollowMemberLink(this HtmlHelper helper,string userID,string userMaster)
        //{
        //    UEFRepository repository = new UEFRepository();
        //    if(!repository.CheckFollowMember(userMaster,userID))
        //    {
        //        string s="<a href=\"/Member.aspx/FollowMember?userID={0}\"/>Theo dõi tin của {1} gởi</a>";
        //        s = string.Format(s, userID,repository.FindMemberById(userID).DisplayName);
        //        return s;
        //    }
        //    string t = "<a href=\"/Member.aspx/NotFollowMember?userID={0}\"/>Hủy theo dõi tin của {1} gởi</a>";
        //    t = string.Format(t, userID, repository.FindMemberById(userID).DisplayName);
        //    return t;
        //}
    }
}